/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt;

import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author ADAM
 */
public class MySqlDBConnector extends TemplateDBConnector{
    private Connection mConn;
    private String mUrl, mUserName, mPassword;
    int mSeqIter = -1;
    Iterator<String> mChromosomeIter;
    List<String> mSeqList;
    List<String> mChromosomesList;
    public MySqlDBConnector(String pmUrl,String pmUserName,String pmPassword)
    {       
        mUrl = pmUrl;
        mUserName = pmUserName;
        mPassword = pmPassword;
        mSeqList = new ArrayList<>();
        connect();
//        mUrl = "jdbc:mysql://localhost:3306/koaladb";
//        mUserName = "root";
//        mPassword = "adam";
    }
    public boolean connect()
        {
            try 
            {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } 
            catch (InstantiationException e) 
            {
                e.printStackTrace();
                return false;
            } 
            catch (IllegalAccessException e) 
            {
                    e.printStackTrace();
                    return false;
            } 
            catch (ClassNotFoundException e) 
            {
                    e.printStackTrace();
                    return false;
            }

            try 
            {
                    mConn = DriverManager.getConnection(mUrl, mUserName, mPassword);
            } 
            catch (SQLException e) 
            {
                e.printStackTrace();
                System.out.println("Bład połączenia z bazą danych");
                return false;
            }
            System.out.println("Połączyłem sie z bazą danych");
            return true;
        }
        /**
         * RozĹ‚Ä…cza z bazÄ… danych
         */
        public boolean disconnect()
        {
            try
            {
                mConn.close();
                return true;
            } 
            catch (SQLException e) 
            {
                System.err.println(e.toString());
                return false;
            }
        }
        
        public Connection getConnection()
        {
            return mConn;
        }

    @Override
    protected List<String> formatujZapytanie(int pmIdOrganizmu) 
    {
        if(mChromosomesList == null)
        {
            String lvQuery = "select chromosomeID from chromosomes where OrganismId = "+pmIdOrganizmu;
            mChromosomesList = getRow(lvQuery);
            if(!mChromosomesList.isEmpty())
            {
                mChromosomeIter = mChromosomesList.iterator();
            }else
            {
                return null;
            }
        }
        if(mSeqIter == -1 || mSeqIter >= mSeqList.size())
        {
            if(mChromosomeIter.hasNext())
            {
                String lvChromosome = mChromosomeIter.next();
                String lvQuery = "select seq from sequences where seqid in (select queryseqid from alignments where jobid in (select jobid from jobs where OrganismId = " +
                         pmIdOrganizmu + " and chromosomeid = " + lvChromosome + " ));";
                mSeqList = getRow(lvQuery);
                if(mSeqList.size() > 0)
                mSeqIter = 0;
                mSeqIter = -1;
                //lvResult = zwrocSekwencje(mIlePobrac,mSeqList);
                return mSeqList;             
            }else
            {
                return null;
            }
           
        }else
        {
            mSeqIter = -1;
            //lvResult = zwrocSekwencje(mIlePobrac,mSeqList);
            return mSeqList;
        }
    }
    private ArrayList<String> getRow(String pmQuery)
    {
        ArrayList<String> lvWynik = new ArrayList<String>();
        Statement lvStatement;
        ResultSet lvRs;
        try 
        {
            lvStatement = mConn.createStatement();
            lvRs = lvStatement.executeQuery(pmQuery);
            //sprawdzamy czy istnieje rekord o takim id 
            while(lvRs.next())
            {
                lvWynik.add(lvRs.getString(1));

            }
        } 
        catch (SQLException e) 
        {
            System.err.println(e.toString());
        }
        finally
        {

        }

        return lvWynik;
    }
    private String zwrocSekwencje(int pmIlePobrac,List<String> pmList)
    {
        StringBuilder lvResult = new StringBuilder("");
        while(mSeqIter<pmList.size())
        {
            String lvStr = pmList.get(mSeqIter);
            if(lvResult.length()<pmIlePobrac)
            {
                if(lvStr.length() + lvResult.length() <= pmIlePobrac)
                {
                    lvResult.append(lvStr);
                }else
                {
                    pmList.set(mSeqIter, lvStr.substring(pmIlePobrac-lvResult.length()));
                    lvResult.append(lvStr.substring(0, pmIlePobrac-lvResult.length()));
                    
                    return lvResult.toString();
                }
            }
            mSeqIter++;
        }
        return lvResult.toString();
    }
}
